import { ApiRes, Article } from '@/type'
import { http } from '@/utils'
import { makeAutoObservable } from 'mobx'

class AriticleStore {
  articleData: Article
  hasMore: boolean
  constructor() {
    makeAutoObservable(this)
    this.articleData = { results: [], pre_timestamp: '' } as Article
    this.hasMore = true
  }
  // 更新
  refreshList = async (channelId: number) => {
    const res = await http.get<ApiRes<Article>>('/articles', {
      params: {
        channel_id: channelId,
        timestamp: Date.now(),
      },
    })
    // 就要新数据
    this.articleData = res.data.data
  }

  // 获取文章列表
  getList = async (channelId: number) => {
    const res = await http.get<ApiRes<Article>>('/articles', {
      params: {
        channel_id: channelId,
        timestamp: this.articleData.pre_timestamp || Date.now(),
      },
    })
    // 数据拼接
    this.articleData.results = [
      ...this.articleData.results,
      ...res.data.data.results,
    ]
    // 记录一下上一次的时间戳 作为下一次数据的请求参数
    this.articleData.pre_timestamp = res.data.data.pre_timestamp
  }
}

export default AriticleStore
